<template>
<div id="Carousel">
  <div class="block">
    <el-carousel  trigger="click" height="900px" :autoplay="true">
      <el-carousel-item v-for="(item,index) in imgs" :key="index">
        <img :src="item" class="bac_img">
      </el-carousel-item>
    </el-carousel>
  </div>
</div>
</template>

<script>

export default {
  name: "Carousel",
  data() {
    return {
      imgs: ['http://pic.aasee.com.cn/wallhaven-qzrxx5.jpg',
             'http://pic.aasee.com.cn/wallhaven-we59gr.jpg'],
      calcRateValue: 0
    }
  },
  methods: {
    calcRate() {
      let img = document.querySelector('.bac_img');
      let l = img.height;
      let w = img.width;
      this.calcRateValue=l/w;
    },
    resize() {
      let clientW=document.documentElement.clientWidth;
      let h=clientW*this.calcRateValue;
      document.querySelector('.el-carousel').style.height=h+'px'
      document.querySelector('.el-carousel__container').style.height=h+'px'
    }
  },
  mounted() {
    this.calcRate()
    this.resize()
    //生成一个监听器：窗口发生变化从新计算计算一遍缩放比例
    window.addEventListener('resize', this.resize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resize)
  }
}
</script>



<style scoped>
  .el-carousel {
    overflow: hidden;
  }

  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }


  img {
    width: 100%;
  }
</style>